<?php

namespace app\index\controller;
use think\Controller;
use app\common\controller\WebFront;
use fast\third\Wechat;
use app\common\model\Userwechat;
use app\common\model\User;
use think\Exception;
use think\Session;

define ('TOKEN','av3gdfgjdq312j1wvvottsypzlkrkcu5');

class Index extends Controller
{


    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
    	
	   
    }
	/**
	 * @return int
	 * 判断是否为微信浏览器
	 */
	public function isweixin(){
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		if (strpos($user_agent, 'MicroMessenger') === false) {
			$result = array("status"=>-1,"msg" => "非微信客户端");
			return json($result);
		} else {
			$result = array("status"=>1,"msg" => "微信客户端");
			return json($result);
		}
	}
	/**
	 * @return \think\response\Json|void
	 * 微信授权登录
	 */
    public function wechatauh(){
	    $wechat=new Wechat();
	    $callurl= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'/index/index/wechatauh';
	  
	    if(session('?userinfo')){
		    $result = array("status"=>1,"wechatuser" => session('userinfo'));
		    return json($result);
	    }
	    else{
		    if(isset($_GET['code'])) {
			    $data = $wechat->getUserInfo1();
			    if (isset($data['errcode']) || !isset($data['openid'])) {
				    $this->redirect($wechat->getAuthorizeUrl1($callurl));
			    }
			    if (!session('?userinfo')) {
				    $this->user = Userwechat::get($data['openid']);
				    if (!$this->user) {
					    $usertemp = new User;
					    $usertemp->username = $data['nickname'];
					    $usertemp->nickname = $data['nickname'];
					    $usertemp->avatar = $data['headimgurl'];
					    $result = $usertemp->save();
					    if (!$result) {
						    $this->returnerror('新增用户信息失败！');
					    }
					    $data['user_id'] = $usertemp->id;
					    $result = db('userwechat')->insert($data);
					    if (!$result) {
						    return $this->returnerror('新增微信用户失败！');
					    }
				    } else {
						    $usertemp = User::get($this->user->user_id);
						    $usertemp->username = $data['nickname'];
						    $usertemp->nickname = $data['nickname'];
						    $usertemp->avatar = $data['headimgurl'];
						    $usertemp->save();
						    $userchatmodel=new Userwechat();
					     $result= $userchatmodel->data($data)->update();
					    $data['user_id']=$this->user->user_id;
					    if (!$result) {
						    return $this->returnerror('更新微信用户失败！');
					    }
				    }
			    } else {
				    if (null == session('userinfo')) {
					    session('userinfo', $data);
				    }
			    }
			    session('userinfo',$data);
			    $result = array("status"=>1,"wechatuser" =>$data);
			    return json($result);
		    }
		    else{
			    $this->redirect($wechat->getAuthorizeUrl1($callurl));
		    }
		
	    }
	
    }
	
	public function returnerror($msg=null){
    	if(!isset($msg)){
		    $msg= "系统错误，请联系管理员！";
	    }
		$result = array("status"=>-1,"msg" => $msg);
		return json($result);
	}
	public function returndata(){
		$result = array("status"=>-1,"msg" => "系统错误，请联系管理员！");
		return json($result);
	}
	
	/**
	 * @return \think\response\Json
	 * 幻灯片展示
	 */
	public function banner(){
		$bannermodel = model('Banner');
		$data = $bannermodel
			->order('weight')
			->limit(3)
			->field('img,url')
			->select();
		if($data){
			$result = array("success"=>1,"data" => $data);
		}
		else{
			$result='{ "error": errorcode , }';
		}
		return json($result);
	}
	
	/***
	 * @return \think\response\Json
	 * 周推荐
	 */
	public function weekrecommend(){
		$weekmodel = model('Staff')->withCount('comment');
		$weekdata = $weekmodel->with('province')->with('grade')
			->order('weight')
			->limit(3)
			->select();
		
		foreach ($weekdata as $key=>$value){
			$data[$key]['id']=$value['id'];
			$data[$key]['name']=$value['name'];
			$data[$key]['region']=$value['province']['name'];
			$data[$key]['age']=birthday($value['birthday']);;
			$data[$key]['star']=$value['grade']['name'];
			$data[$key]['praise']=$value['comment_count'];
			$data[$key]['imgurl']=$value['id'];
			
		}
		if($data){
			$result = array("success"=>1,"data" => $data);
		}
		else{
			$result='{ "error": errorcode , }';
		}
		return json($result);
	}
	
	
	/***
	 * @return \think\response\Json
	 * 员工列表
	 */
	public function allstaff(){
	    $grade_id= $this->request->get("grade_id", 0);
		$offset = $this->request->get("pageNumber", 0);
		$limit = $this->request->get("pageSize", 10);
		$staffmodel = model('Staff')->withCount('comment');
        $wdata='';
		if($grade_id!=0) $wdata.='grade_id='.$grade_id;
		$staffdata = $staffmodel->with('province')->with('grade')
			->order('weight')
			->limit($offset,$limit)
            ->where($wdata)
			->select();
		$total = $staffmodel
			->order('weight')
            ->where($wdata)
			->count();
		foreach ($staffdata as $key=>$value){
			$data[$key]['id']=$value['id'];
			$data[$key]['name']=$value['name'];
			$data[$key]['summary']=$value['tag'];
			$data[$key]['date']=(int)$value['serverdays_id'];
			$data[$key]['star']=(int)$value['grade_id'];
			$data[$key]['price']=(int)$value['price'];
			$data[$key]['imgurl']=$value['avatar'];
			
		}
		if($data){
			$result = array("success"=>1,"total"=>$total,"data" => $data);
		}
		else{
			$result='{ "error": errorcode , }';
		}
		return json($result);
	}
	
}
